//
//  DemoTuples.swift
//  swiftDemo
//
//  Created by JIENING ZHANG on 2020/1/8.
//  Copyright © 2020 lovivid. All rights reserved.
//

import UIKit

class DemoTuples: NSObject {
    class func computeWeekInfo(_ year:Int, _ month:Int, _ day:Int) -> (weekInYear:Int, dayInWeek:Int) {
        // 一顿计算猛如虎
        return (2, 3)
    }
    
    class func demoLogic() {
        // 元组可以嵌套
        let dateTuple = (year:2020,
                         isLeap:true,
                         month:(1, "January", "Jan"),
                         day:8,
                         week:(weekInYear:2, dayInWeek:3, fullName:"Wednesday", shortName:"Wed"))
        print("dateTuple=\(dateTuple)");
        
        // 按下标访问元组某个元素
        let monthTuple = dateTuple.2
        print("monthTuple=\(monthTuple)");
        
        // 按名访问元组某个元素
        let weekTuple = dateTuple.week
        print("weekTuple=\(weekTuple)");
        
        // 元组为左值后，各元素马上可用
        let (monthNum, monthFullName, monthShortName) = monthTuple;
        print("monthNum=\(monthNum)", "monthFullName=\(monthFullName)", "monthShortName=\(monthShortName)")
        
        // 元组为左值, 部分获取的情况
        let (weekNum, _, weekDayName, _) = weekTuple;
        print("weekNum=\(weekNum)", "weekDayName=\(weekDayName)")
        
        // 元组作为函数返回值的情况
        let (weekInYear, dayInWeek) = computeWeekInfo(2020, 1, 8)
        print("weekInYear=\(weekInYear)", "dayInWeek=\(dayInWeek)")
        
/*
输出:
         
dateTuple=(year: 2020, isLeap: true, month: (1, "January", "Jan"), day: 8, week: (weekInYear: 2, dayInWeek: 3, fullName: "Wednesday", shortName: "Wed"))
monthTuple=(1, "January", "Jan")
weekTuple=(weekInYear: 2, dayInWeek: 3, fullName: "Wednesday", shortName: "Wed")
monthNum=1 monthFullName=January monthShortName=Jan
weekNum=2 weekDayName=Wednesday
weekInYear=2 dayInWeek=3
*/
    }
}
